﻿using System;
using System.Text.RegularExpressions;

namespace _5E.Utils.CheckHelper
{
    /// <summary>
    /// 程序说明：正则验证
    /// 创建作者：ahl
    /// 创建时间：2012-04-02
    /// </summary>
    public class RegexVerification : IVerification
    {
        #region 变量

        private string m_Rule = null;

        private string m_Value = null;

        #endregion

        #region 属性

        private string m_ErrorMessage = null;
        public string ErrorMessage { get { return this.m_ErrorMessage; } }

        #endregion

        #region 构造函数

        public RegexVerification(string rule, string value, string errorMessage)
        {
            this.SetRule(rule);
            this.m_Value = value;
            this.m_ErrorMessage = errorMessage;
        }

        #endregion

        #region 实例方法

        public virtual bool Validate()
        {
            var reg = new Regex(this.m_Rule);
            var validateResult =  reg.IsMatch(this.m_Value);
            return validateResult;
        }

        private void SetRule(string rule)
        {
            if (string.IsNullOrEmpty(rule))
            {
                throw new ArgumentNullException("验证规则不可空!");
            }
            this.m_Rule = rule;
        }

        #endregion
    }
}
